package com.cqs.learning.spring.cloud.app.controller;

import com.cqs.learning.spring.cloud.app.bean.vo.QueryContextInfo;
import com.cqs.learning.spring.cloud.app.client.IConsumer;
import com.cqs.learning.spring.cloud.app.context.QueryContext;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by li on 20-3-1.
 */
@RequestMapping
@RestController
@Log4j2
@RefreshScope
public class ConsumerController {

    //    @Autowired
    private IConsumer consumer;


    @Value(value = "${server.port}")
    private String serverPort;
    @Value(value = "${from}")
    private String from;


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/cnt")
    public String getCnt() {
        String count = consumer.cnt();
        log.info("remote service return cnt: {}", count);
        return "remote service return cnt: " + count;
    }


    private AtomicInteger integer = new AtomicInteger(1);

    /**
     * ribbon 客户端负载策略验证
     *
     * @return
     */
    @GetMapping("/ribbon")
    public ResponseEntity ribbon() {
        int id = integer.getAndAdd(1);
        QueryContextInfo info = new QueryContextInfo();
        log.info(id + " 查询query:" + QueryContext.getContext());
        QueryContext.setContext(info);

        info.setQueryId(System.currentTimeMillis());

        log.info(id + "设置queryId:" + info.getQueryId());
        try {
            TimeUnit.MILLISECONDS.sleep(2000);
            Executors.newSingleThreadExecutor().submit(new Runnable() {
                @Override
                public void run() {
                    test(info, id);
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://producer-service/producer/cnt", String.class);
        log.info("ribbon结果：{}", forEntity);
        return forEntity;
    }

    private void test(QueryContextInfo info, int count) {
        while (true) {
            log.info(count + "info:" + info.getQueryId());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }


    /**
     * 从配置中心读取配置文件
     *
     * @return
     */
    @GetMapping("/config")
    public String config() {
        log.info("从配置中心读取得结果：{}", from);
        return from;
    }
}
